Ext.onReady(function() {
	Ext.QuickTips.init();
	$("#main").panel({
		width:'100%',
		height:700
	});
	
	var doc = Ext.get('prof');
	var add = Ext.get('add');
	var remove = Ext.get('remove');
	var editor;
	try {
		editor = UE.getEditor('editor', {
					initialFrameWidth : '100%',
					initialFrameHeight : 300
				});
	} catch (e) {
		console.info(e.message);
	}
	$("select").keydown(function(e) {
				// console.info(e);
				return false;
			});
	// 给出未选择该学生的理由
	$(".setReason").click(function() {
		// alert('click');
		var intentId = $(this).attr("name");
		var reasonWin = Ext.create('Ext.window.Window', {
			title : '给出未选择理由',
			width : 400,
			height : 200,
			padding : 1,
			modal : true,
			// iframe : true,
			renderTo : Ext.getBody(),
			items : [{
						xtype : 'textareafield',
						name : 'reason',
						fieldLabel : '理由',
						allowBlank : false,
						width : 375,
						height : 140,
						anchor : '100%'
					}],
			buttons : [{
				xtype : 'button',
				text : '确定',
				handler : function(e) {
					var reasonEl = Ext.select('[name=reason]', true).elements[0];
					// console.info(reasonEl);
					var reason = reasonEl.dom.value;
					if (Ext.String.trim(reason) == "") {
						return false;
					}
					console.info(reason + "," + intentId);
					Ext.Ajax.request({
								url : 'setReason',
								params : {
									reason : reason,
									intentId : intentId
								},
								success : function(r) {
									var res = r.responseText;
									if (res == "success") {
										Ext.Msg.alert('提示', '保存成功!');
										location.replace(location.href);
									} else {
										Ext.Msg.alert('提示', '保存失败！');
									}
								}
							});
				}
			}, {
				xtype : 'button',
				text : '取消',
				handler : function() {
					reasonWin.close();
				}
			}]
		}).show();
	});
	// 选择学生
	var selectStu = $(".selectStu");
	if (selectStu != null) {
		$(".selStu").click(function() {
					var confir = confirm("确认选择该学生？");
					if (confir) {
						var stu_id = $(this).siblings(".stu_id").val();
						var proj_id = $(this).siblings(".proj_id").val();
						Ext.Ajax.request({
									url : 'selectStu',
									params : {
										stu_id : stu_id,
										proj_id : proj_id
									},
									success : function(r) {
										var res = r.responseText;
										if (res == 'success') {
											alert('成功选择学生');
											// window.location = 'teacher_home';
											location.replace(location.href);
										} else {
											alert('请重试！');
										}
									},
									failure : function(r) {
										alert("error");
									}
								});
					} else {
						return false;
					}
				});
	}

	$(".setReason").click(function() {

			});

	var selectedProf = Ext.get('selectedProf');
	if (!(doc == null | add == null | selectedProf == null)) {
		// 处理专业选择请求
		add.dom.onclick = function() {
			var prof_name = doc.getValue();
			// console.info(prof_name);
			if (prof_name != null && prof_name != "") {
				if (!containsItems('selectedProf', prof_name)) {
					RemoveSelectItem('prof', prof_name);
					addSelectItem('selectedProf', prof_name);
				}
			}
		};
		// 取消选择专业
		remove.dom.onclick = function() {
			var remove_item = selectedProf.getValue();
			if (!containsItems('prof', remove_item)) {
				if(Ext.String.trim(remove_item)!='')
				{
					RemoveSelectItem('selectedProf', remove_item);
					addSelectItem('prof', remove_item);
				}
			}
		};

		// 获取提交按钮对象
		var submit = Ext.get('submit');
		submit.dom.onclick = function() {
			var prof_names = getSelectListValues('selectedProf');
			var name = Ext.get('title').dom.value;
			var desc = Ext.get('desc').dom;
			var description = desc.value = editor.getContent();
			var remark = Ext.get('remark').dom.value;
			var fileName = Ext.get('fileName').dom.value;
			// 检查信息完整性
			if (!validateArgument(prof_names, name, description)) {
				alert('请填写完整信息！');
				return;
			}

			Ext.Ajax.request({
						url : 'publish_project',
						params : {
							name : name,
							prof_names : prof_names,
							description : description,
							remark : remark,
							fileName : fileName,
							syteacher : Ext.get('sytea').getValue()
						},
						disableCaching : true,
						success : function(f) {
							var returnValue = f.responseText;
							if (returnValue == 'success') {
								alert('添加成功！');
								window.location = "teacher_home";
							} else {
								alert('操作失败！请重试');
							}
						},
						failure : function() {
							alert('系统错误！请重试');
						}
					});
		};

	}
	// 删除题目
	$(".delProj").click(function() {
				if (confirm('确定删除该项？')) {
					var id = $(this).attr("name");
					Ext.Ajax.request({
								url : 'delete_project_' + id,
								success : function(response) {
									var responseText = response.responseText;
									if (responseText == 'success') {
										alert('已删除');
										window.location = 'teacher_home';
									} else {
										alert('删除失败！');
										return false;
									}
								}
							});
					return false;
				} else {
					return false;
				}
			});

	// 获取修改题目DOM对象
	var update_proj = Ext.get('update');
	if (update_proj != null) {
		update_proj.dom.onclick = function() {
			var proj_id = Ext.get('id').dom.value;
			var proj_name = Ext.get('title').dom.value;
			var desc = editor.getContent();
			var proj_desc = Ext.get('desc').dom.value;
			var proj_remark = Ext.get('remark').dom.value;
			// alert(proj_id+","+proj_name+","+proj_desc);
			if (validateArgument(proj_id, proj_name, desc)) {
				Ext.Ajax.request({
							url : 'update_project',
							params : {
								id : proj_id,
								name : proj_name,
								description : desc,
								remark : proj_remark
							},
							success : function(response) {
								var rv = response.responseText;
								if (rv == 'success') {
									alert('修改成功！');
									window.location = 'teacher_home';
								}
							}

						});
			} else {
				alert('请正确填写更正信息');
			}
		};
	}

	// 获取旧密码输入框对象
	var oldpwd = Ext.get('oldPassword');
	if (oldpwd != null) {
		oldpwd.dom.onblur = function() {
			var old = oldpwd.dom.value;
			if (old == '' || old == null) {
				return;
			}
			Ext.Ajax.request({
						url : 'validatePassword',
						params : {
							password : old
						},
						success : function(response) {
							var s = response.responseText;
							if (s != 'success') {
								Ext.get('pwdinfo')
										.applyStyles('display:inline;');
							} else {
								Ext.get('pwdinfo').applyStyles('display:none;');
							}
						},
						failure : function() {
							alert('failure');
						}
					});
		};
	}

	// 获取密码修改提交按钮对象
	var modify_pwd = Ext.get('mod_pwd');

	if (!IsNull(modify_pwd)) {
		var newpass = Ext.get('first');
		modify_pwd.dom.onclick = function() {
			var first = Ext.get('first');
			var second = Ext.get('second');
			if (first.dom.value != second.dom.value) {
				alert('两次输入的密码不一致!');
				return;
			}
			Ext.Ajax.request({
						url : 'modify_password',
						params : {
							password : newpass.dom.value
						},
						success : function(response) {
							alert('修改成功！请重新登录');
							window.location = 'teacher_home';
						}
					});
		};
	}
	/**
	 * 学生或教师信息查询
	 */
	$("#subQuery").click(function() {

		var type = $("#type").val();
		var keyword = $("#query").val();
		if (Ext.String.trim(keyword) == "") {
			return false;
		}
		Ext.Ajax.request({
			url : 'queryInfo',
			params : {
				type : type,
				keyword : keyword
			},
			success : function(r) {
				var user = Ext.JSON.decode(r.responseText);
				if (type == 'teacher') {
					$(".info").text("部门");
					$("#depart").text(user.department.name);
					$("#ac").text(user.department.college.name);
					var attachHead = "<th width='20%'>电话</th><th width='20%'>qq</th><th>职称</th>";
					$("#attachHead").html(attachHead);
					$("#attachInfo").html("<td>" + user.telephone + "</td><td>"
							+ user.qq + "</td><td>" + user.title + "</td>");
					$("#detailInfo").css("display", "block");
					$("#detailInfo").text(user.info);
					$("#di").css("display", "block");
				} else {
					console.info(user.grade);
					var attachHead = "<th>年级</th><th>电话</th><th>qq</th>";
					$("#attachHead").html(attachHead);
					$(".info").text("专业");
					$("#depart").text(user.classInfo.branch.prof.name);
					$("#ac")
							.text(user.classInfo.branch.prof.department.college.name);
					$("#attachInfo").html("<td>" + user.grade + "</td><td>"
							+ (user.telephone == null ? '' : user.telephone)
							+ "</td><td>" + (user.qq == null ? '' : user.qq)
							+ "</td>");
					$("#di").css("display", "none");
				}
				$("#no").text(user.no);
				$("#name").text(user.name);
				$("#gender").text(user.gender);

				$("#personInfo").css("display", "block");
			}
		});
	});

	if ($("#sytea")) {
		Ext.Ajax.request({
					url : 'getTeacherList',
					success : function(r) {
						var teacherList = Ext.JSON.decode(r.responseText);
						// console.info(teacherList);
						var options = "<option value='0' selected>请选择</option>";
						Ext.Array.each(teacherList, function(val) {
									options += "<option value='" + val.id
											+ "'>" + val.name + "</option>";
								});
						$("#sytea").html(options);
						// alert($("#syt").val());
						if ($("#syt")) {
							$("#sytea").val($("#syt").val());
						}

					}
				});
	}

	// 获取状态选择框对象
	var proj_status_select = Ext.get('proj_status');
	if (!IsNull(proj_status_select)) {
		proj_status_select.dom.onchange = function() {
			var status = proj_status_select.dom.value;
			if (status != '请选择状态') {
				Ext.Ajax.request({
					url : 'getProjectByStatus',
					params : {
						status : proj_status_select.dom.value
					},
					success : function(response) {
						var projects = response.responseText;
						var p = Ext.JSON.decode(projects);
						//console.info(p);
						var proj_teacher = Ext.get('projList');
						proj_teacher.addCls("");
						proj_teacher.dom.innerHTML = "";
						if (p.length == 0) {
							proj_teacher.dom.innerHTML = "无符合条件的数据";
							return;
						}
						var result = "";
						var title = '<tr><td>专业名称</td><td>题目名称</td><td>操作</td></tr>';
						var profs = "";
						for (var i = 0; i < p.length; i++) {
							for (var j = 0; j < p[i].profs.length; j++) {
								var prof_name = p[i].profs[j].profBranchName;
								profs += prof_name + " ";

							}
							// 去除获得的专业名称中的前后空格
							var profs = Ext.String.trim(profs);
							var status = '';
							console.info(p[i].status);
							switch(p[i].status){
								case 'NOT_AUTH':
								 status='未审核';
								break;
								case 'AUTHERIC_FAILED':
								status = '审核失败';
								break;
								case 'PASSED':
								status = '审核通过';
								break;
								default:
								break;
							}
							result += "<tr><td>" + profs + "</td><td>"
									+ p[i].name + "</td><td>"
									+status+"</td><td>"
									+ "<a href='project_detail_" + p[i].id
									+ "' class='btn btn-success'>详细</a> | ";
							if ($("#enup").val() == 'true') {
								result += "<a class='btn btn-info' href='update_project_" + p[i].id+ "'>修改</a> | ";
								//console.info("<a class='btn btn-info' href='update_project_" + p[i].id+ "'>修改</a> | ");
							}
							result += "<a href='delete_project_" + p[i].id
									+ "' class='delProj btn btn-warning' name='" + p[i].id
									+ "'>删除</a>";
							if ((p[i].taskBook == null || p[i].taskBook == "")
									&& $("#entask").val() == 'true') {
								result += "|<a href='javascript:void(0);' class='upTask btn btn-info'>任务书</a>";
							}
							result += "</td></tr>";
							//alert(result);
							profs = "";// 清空专业名称，供下一个project对象使用
						}
						proj_teacher.dom.innerHTML = result;
					}
				});
			}
		};
	}
	$("#filter").click(function() {
		var projStatus = $("#proj_status").val();
		var keyword = $("#proj_name").val();
		Ext.Ajax.request({
			url : 'queryProjectByKeywordOrStatus',
			params : {
				status : projStatus,
				keyword : keyword
			},
			success : function(r) {
				var projects = r.responseText;
				var p = Ext.JSON.decode(projects);
				// console.info(p);
				var proj_teacher = Ext.get('projList');
				// proj_teacher.addCls("");
				proj_teacher.dom.innerHTML = "";
				if (p.length == 0) {
					proj_teacher.dom.innerHTML = "无符合条件的数据";
					return;
				}
				var result = "";
				var profs = "";
				for (var i = 0; i < p.length; i++) {
					for (var j = 0; j < p[i].profs.length; j++) {
						var prof_name = p[i].profs[j].profBranchName;
						// alert(i+":"+prof_name);
						profs += prof_name + " ";
					}
					// 去除获得的专业名称中的前后空格
							var profs = Ext.String.trim(profs);
							var status = '';
							console.info(p[i].status);
							switch(p[i].status){
								case 'NOT_AUTH':
								 status='未审核';
								break;
								case 'AUTHERIC_FAILED':
								status = '审核失败';
								break;
								case 'PASSED':
								status = '审核通过';
								break;
								default:
								break;
							}
							result += "<tr><td>" + profs + "</td><td>"
									+ p[i].name + "</td><td>"
									+status+"</td><td>"
									+ "<a href='project_detail_" + p[i].id
									+ "' class='btn btn-success'>详细</a> | ";
							if ($("#enup").val() == 'true') {
								result += "<a class='btn btn-info' href='update_project_" + p[i].id+ "'>修改</a> | ";
								//console.info("<a class='btn btn-info' href='update_project_" + p[i].id+ "'>修改</a> | ");
							}
							result += "<a href='delete_project_" + p[i].id
									+ "' class='delProj btn btn-warning' name='" + p[i].id
									+ "'>删除</a>";
							if ((p[i].taskBook == null || p[i].taskBook == "")
									&& $("#entask").val() == 'true') {
								result += "|<a href='javascript:void(0);' class='upTask btn btn-info'>任务书</a>";
							}
							result += "</td></tr>";
							//alert(result);
							profs = "";// 清空专业名称，供下一个project对象使用
				}
				$("#projList").html(result);
			}
		});
	});

	// 获取教师信息修改DOM对象
	var upTeacher = Ext.get('updateBaseInfo');
	if (!IsNull(upTeacher)) {
		upTeacher.dom.onclick = function() {
			var name = Ext.get('name').dom.value;
			var tel = Ext.get('tel').dom.value;
			var qq = Ext.get('qq').dom.value;
			if (!validateArgument(name, tel, qq)) {
				alert('请填写完整信息!');
				return;
			}
			Ext.Ajax.request({
						url : 'updateInfo',
						params : {
							name : name,
							tel : tel,
							qq : qq
						},
						success : function(response) {
							var text = response.responseText;
							if (text == 'success') {
								alert('修改成功！');
								window.location = 'teacher_home';
							} else {
								alert('修改失败！');
							}
						}

					});
		};
	}
	
});
/**
 * 获取下拉列表的所有可选值的数组
 * 
 * @param {}
 *            id
 * @return {}
 */
function getSelectListValues(id) {
	var result = [];
	var options = document.getElementById(id).options;
	$.each(options,function(k,v){
		result.push(v.label);
	});
	
	return result;
};
/**
 * 获取下拉列表中的所有子元素组成的数组
 * 
 * @param {}
 *            id
 * @return {}
 */
function getArrayOfSelectItem(id) {
	var select = Ext.get(id);
	var html = select.dom.innerHTML;
	var option_arr = new Array();
	option_arr = html.split("</option>");
	for (var i = 0; i < option_arr.length; i++) {
		if (option_arr[i] != "" && option_arr[i] != null) {
			option_arr[i] += "</option>";
			option_arr[i] = Ext.String.trim(option_arr[i]);// 去掉元素字符串中的前后空格
		}
	}
	return option_arr;
};
/**
 * 将数组换为字符串
 * 
 * @param {}
 *            arr
 * @return {}
 */
function ArrayToString(arr) {
	var result = "";
	for (var i = 0; i < arr.length; i++) {
		result += Ext.String.trim(arr[i]);
	}
	return result;
};

/**
 * 检验下拉列表中是否包含该元素
 * 
 * @param {}
 *            selectId
 * @param {}
 *            item
 * @return {Boolean}
 */
function containsItems(selectId, item) {
	var items = getSelectListValues(selectId);
	for (var i = 0; i < items.length; i++) {
		if (items[i] == item) {
			return true;
		}
	}
	return false;
}
/**
 * 将指定元素item添加到下拉列表中
 * 
 * @param {}
 *            selectId
 * @param {}
 *            item
 */
function addSelectItem(selectId, item) {
	var select = Ext.get(selectId);
	var items = getArrayOfSelectItem(selectId);
	if (!containsItems(selectId, item))
		items.push("<option>" + item + "</option>");
	select.update(ArrayToString(items));
}
/**
 * 从下拉列表中删除指定元素
 * 
 * @param {}
 *            selectId
 * @param {}
 *            item
 */
function RemoveSelectItem(selectId, item) {
	var items = getSelectListValues(selectId);
	var index = 0;
	$.each(items,function(k,v){
		if(v==item)
			index=k;
	});
	document.getElementById(selectId).options.remove(index);
}

/**
 * 验证函数参数是否非空或者为空字符串
 * 
 * @return {Boolean}
 */
function validateArgument() {
	for (var i = 0; i < arguments.length; i++) {
		if (arguments[i] == null || arguments[i] == "") {
			return false;
		}
	}
	return true;
}

/**
 * 检验对象是否为null
 * 
 * @param {}
 *            obj
 * @return {}
 */
function IsNull(obj) {
	return obj == null;
}
/**
 * 显示数据导入窗口
 */
function showImportWindow() {
	var importForm = Ext.create('Ext.form.Panel', {
		title : '数据导入',
		width : 400,
		bodyPadding : 10,
		frame : true,
		layout : 'fit',
		items : [{
					xtype : 'filefield',
					name : 'filePath',
					fieldLabel : '文件',
					labelWidth : 50,
					msgTarget : 'side',
					allowBlank : false,
					anchor : '100%',
					buttonText : '选择文件'
				}],

		buttons : [{
					text : '确定',
					handler : function() {
						var form = this.up('form').getForm();
						if (form.isValid()) {
							form.submit({
										url : 'batchAddProject',
										params : {
											cols : $("#dataImportCols").val()
										},
										waitMsg : '正在导入数据......',
										success : function(fp, o) {
											Ext.Msg.alert('Success', '数据导入成功!');
											importWin.close();
											location.reload();
										}
									});
						}
					}
				}]
	});

	var importWin = Ext.create("Ext.window.Window", {
				title : '',
				modal : true,
				height : 150,
				width : 400,
				items : [importForm]
			}).show();
}
/**
 * 上传题目附件
 */
$(function() {
	$("#attach_file").change(function() {
				$.ajaxFileUpload({
							url : 'uploadAttachFile', // 需要链接到服务器地址
							secureuri : false,
							fileElementId : 'attach_file', // 文件选择框的id属性
							dataType : 'text', // 服务器返回的格式，可以是json
							success : function(data, textStatus) { // 相当于java中try语句块的用法
								$(this).click(function() {
											return false;
										});
								$("#fileName").val(data);
							},
							error : function(data, status, e) { // 相当于java中catch语句块的用法
							}
						});

			});
	$("#importFile").click(function() {
				$("#importCols").window({
							title : '设置数据列'
						}).window("open");
			});
	$("input[name=cols]").attr("checked", true);
	$("#setCols").click(function() {
				var colArr = Ext.select('[name=cols]').elements;
				var cols = [];
				var index = 0;
				Ext.Array.each(colArr, function(col) {
							if ($(col).attr("checked") == "checked") {
								cols[index] = $(col).val();
								index++;
							}
						});

				console.info(cols);
				$("#dataImportCols").val(cols);
				$("#importCols").window("close");
				showImportWindow();
			});

	// 导出题目
	$("#exportData").click(function() {
				$("#exportCols").window({
							title : '导出数据列'
						}).window("open");
						$("[name=exportCols]").attr("checked",true);
			});
	// 题目审阅
	$(".sy").click(function() {
				$("#projId").val($(this).attr("name"));
				$("#syDiv").window({
							title : '题目审阅'
						}).window("open");
			});
	$(".yn").eq(0).attr("checked", true);
	$("#status").val("y");
	$("#reasonDiv").css("display", "none");
	$(".yn").change(function() {
				var val = $(this).val();
				$("#status").val(val);
				if (val == 'n') {
					$("#scoreDiv").css("display", "none");
					$("#reasonDiv").css("display", "block");
				} else {
					$("#reasonDiv").css("display", "none");
					$("#scoreDiv").css("display", "block");
				}
			});
	$("#subSy").click(function() {
				var status = $("#status").val();
				var reason = $("#reason").val();
				var projId = $("#projId").val();
				var score = $("#score").val();
				if (parseInt(score) > 40) {
					$(".topic").css("display", "inline");
					// alert("请输入大于60的数字");
					return false;
				}
				if (status == 'n' && Ext.String.trim(reason) == '') {
					$(".reasonTopic").css("display", "inline");
					return false;
				}
				Ext.Ajax.request({
							url : 'syProj',
							params : {
								status : status,
								reason : reason,
								id : projId,
								score : score
							},
							success : function(r) {
								if (r.responseText == 'success') {
									alert('提交成功!');
									location.replace(location.href);
								} else {
									console.info("failed");
								}
							}
						});
			});

	// 上传任务书
	$(".upTask").click(function() {

		var proj_id = $(this).attr("name");
		var upForm = Ext.create('Ext.form.Panel', {
			title : '上传任务书',
			width : 400,
			bodyPadding : 10,
			frame : true,
			layout : 'fit',
			items : [{
						xtype : 'filefield',
						name : 'taskFile',
						fieldLabel : '文件',
						labelWidth : 50,
						msgTarget : 'side',
						allowBlank : false,
						anchor : '100%',
						buttonText : '选择文件'
					}],

			buttons : [{
						text : '确定',
						handler : function() {
							var form = this.up('form').getForm();
							if (form.isValid()) {
								form.submit({
											url : 'uploadTask?proj_id='
													+ proj_id,
											waitMsg : '正在上传任务书......',
											success : function(fp, o) {
												Ext.Msg.alert('Success',
														'任务书上传成功!');
												location.replace(location.href);
											}
										});
							}
						}
					}]
		});

		var upWind = Ext.create("Ext.window.Window", {
					title : '',
					modal : true,
					height : 150,
					width : 400,
					items : [upForm]
				}).show();
	});

	$(".upPlan").click(function() {
		var courseId = $(this).attr("name");
		var planForm = Ext.create(("Ext.form.Panel"), {
					width : 380,
					height : 100,
					bodyPadding : 20,
					frame : true,
					items : [{
								xtype : 'filefield',
								name : 'tplan',
								buttonText : '浏览',
								fieldLabel : '任务书',
								msgTarget : 'side',
								allowBlank : false,
								anchor : '100%',
								// msgTarget: 'side',
								labelWidth : 50
							}],
					buttons : [{
						text : '上传',
						handler : function(fp, action) {
							var form = this.up('form').getForm();
							var file = Ext.select('[name=tplan]', true).elements[0];
							var fileName = file.dom.value;
							// console.info(fileName);

							var extName = fileName.substring(fileName
									.lastIndexOf(".")
									+ 1);
							if (!(extName == "doc" || extName == "docx")) {
								Ext.Msg.alert("警告", "请选择doc/docx类型文件!");
								return false;
							}
							// console.info(nameArr);
							if (form.isValid()) {
								form.submit({
											url : 'upPlanBook',
											params : {
												courseId : courseId
											},
											success : function(fp, action) {
												Ext.Msg.alert("提示", "上传成功!");
												location.replace(location.href);
											}
										});
							}
						}
					}]
				});
		Ext.create("Ext.window.Window", {
					title : '教学计划上传',
					modal : true,
					// frame:true,
					width : 390,
					height : 130,
					items : [planForm],
					renderTo : Ext.getBody()
				}).show();
	});

	// 查看教学计划
	$(".showPlan").click(function() {

		var courseId = $(this).attr("name");
		Ext.Ajax.request({
					url : 'getPlanByCourseId',
					params : {
						courseId : courseId
					},
					success : function(res) {
						var url = res.responseText;
						// console.info(url);
						$("#planFrame").attr("src", url)
								.css("display", "block");
					}
				});
	});
	// 按教师选择学生
	$(".setStu").click(function() {
		var id = $(this).attr("name");
		var stuId = $(this).attr("num");
		var a = this;
		Ext.Ajax.request({
			url : 'GetPassedProject',
			params : {
				id : $("#tid").val()
			},
			success : function(r) {
				var projs = Ext.JSON.decode(r.responseText);
				$("#span" + id)
						.html("<select class='projs' style='height:28px;width:100px;'></select>&nbsp;&nbsp;<a href='javascript:void(0);' class='ok' name='"
								+ stuId + "'>确定</a>");
				var html = "";
				$(a).html("");
				for (var i = 0; i < projs.length; i++) {
					html += "<option value='" + projs[i].id + "'>"
							+ projs[i].name + "</option>";
				}
				$(".projs").html(html);
				$(".ok").click(function() {
							var projId = $(this).siblings(".projs").val();
							// var stuId = $(this).attr("name");
							console.info(projId + "," + stuId);
							Ext.Ajax.request({
										url : 'SelectStuByTeacher',
										params : {
											id : projId,
											stuId : stuId
										},
										success : function(r) {
											var res = r.responseText;
											if (res == 'success') {
												Ext.Msg.alert("提示", "成功选择学生!");
												location.replace(location.href);
											} else {
												alert('系统异常！请重试');
											}
										}
									});
						});
			}
		});
	});

	// 成绩录入
	$(".score").click(function() {
		var id = $(this).attr("name");
		
		console.info(id);
		var scoreForm = Ext.create('Ext.form.Panel', {
					title : '成绩录入',
					bodyPadding : 5,
					width : 240,
					layout : 'anchor',
					url:'setScore',
					defaults : {
						anchor : '100%'
					},
					defaultType : 'textfield',
					items : [{
								fieldLabel : '指导教师成绩',
								name : 'sy',
								allowBlank : false
							}, {
								fieldLabel : '评阅人成绩',
								name : 'py',
								allowBlank : false
							},{
								fieldLabel:'答辩成绩',
								name:'db',
								allowBlank:false
							},{
								xtype:'textarea',
								fieldLabel:'答辩小组成员',
								name:'dbmember',
								allowBlank:false
							}],

					// Reset and Submit buttons
					buttons : [{
								text : '清空',
								handler : function() {
									this.up('form').getForm().reset();
								}
							}, {
								text : '确定',
								formBind : true, // only enabled once the
								disabled : true,
								handler : function() {
									var form = this.up('form').getForm();
									if (form.isValid()) {
										form.submit({
											
											params:{
												id:id
											},
											success : function(form,action) {
												scoreWin.close();
												Ext.Msg.alert("success","成绩录入成功");
												location.replace(location.href);
											},
											failure : function(form,action) {
												Ext.Msg.alert('Failed',"录入失败");
											}
										});
									}
								}
							}]
				});
			var scoreWin = Ext.create("Ext.window.Window",{
				title:'',
				modal:true,
				layout:'fit',
				width:300,
				height:260,
				items:[scoreForm],
				renderTo:Ext.getBody()
			}).show();
	});
	
	$("#getCols").click(function(){
		var exportCols = $("[name=exportCols]");
		var colArr = [];
		$.each(exportCols,function(k,v){
			if($(v).attr("checked")=='checked'){
				colArr.push($(v).val());
			}
		});
		//console.info(colArr);
		$("#dataExportCols").val(colArr);
		Ext.Ajax.request({
			url:'export',
			params:{
				cols:colArr
			},success:function(r){
				var href=r.responseText;
//				console.info(href);
				$("#exportCols").window("close");
				window.open(href);
			}
		});
	});
	//获取最终结果
	if(document.getElementById('lastResult') != null){
		$("#lastResult").click(function(){
			Ext.Ajax.request({
				url:'getLastResult',
				success:function(r){
					var href = r.responseText;
//					console.info(href);
					window.open(href);
				}
			});
		});
	}
});